#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "gui/graphicsmap.h"
#include "gui/graphicscity.h"
#include <QDebug>
#include "genetic/path.h"
#include <QtConcurrent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QGraphicsView *view = ui->graphicsView;
    gMap = new GraphicsMapScene();
    view->setScene(gMap);
    view->setRenderHints(QPainter::Antialiasing);

    map.Read("map.csv");
    qInfo() << map;

    gMap->setMap(&map);
    Path::map = &map;

    Population pop = Population::generate(5);
    qDebug()<<pop;

    foreach (auto item, gMap->items())
    {
        GraphicsCity *gCity = qgraphicsitem_cast<GraphicsCity *>(item);
        if (gCity != nullptr)
        {
            connect(gCity, SIGNAL(pressed(City*)), this,
                    SLOT(updateCityInfo(City*)));
        }
    }

    ga = new GA();
    connect(ga,SIGNAL(update()),this,SLOT(updateGaInfo()));

    show();
    updateViewSize();
    gMap->onSizeChanged();
}

MainWindow::~MainWindow() { delete ui; }

void MainWindow::updateViewSize()
{
    QGraphicsView *view = ui->graphicsView;
    gMap->setSceneRect(0, 0, view->width() * 0.95, view->height() * 0.95);

    qreal diameter = std::max(view->width(), view->height()) * 0.015;
    GraphicsCity::BOUNDINGRECT = QRectF(0, 0, diameter, diameter);
}

void MainWindow::setGaParam()
{
    ga->cProb = ui->spin_cprob->value();
    ga->mProb = ui->spin_mprob->value();
    ga->rRate = ui->spin_rprob->value();
    ga->popSize = ui->spin_popuSize->value();
    ga->gFull = ui->spin_full->value();
    ga->gMax = ui->spin_genNum->value();
    ga->full = ui->checkBox_full->isChecked();
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    updateViewSize();
    gMap->onSizeChanged();
}

void MainWindow::closeEvent(QCloseEvent *event) {}

void MainWindow::on_btn_start_clicked()
{
    QFuture<void> f = QtConcurrent::run(&MainWindow::start, this);
}

void MainWindow::start()
{
    ui->btn_start->setEnabled(false);
    GraphicsCity::LINECOLOR = QColor(255, 0, 0);

    setGaParam();
    ga->start();

    GraphicsCity::LINECOLOR = QColor(0, 255, 0);
    //gMap->showPath(pop.first());
    ui->btn_start->setEnabled(true);
}

void MainWindow::updateCityInfo(City *city)
{
    ui->label_cityName->setText("名称：" + city->name());
    ui->label_cityPos->setText("经纬度：(" + QString::number(city->lon()) +
                               "," + QString::number(city->lan()) + ")");
}

void MainWindow::updateGaInfo()
{
    ui->label_generation->setText(QString::number(ga->gCurrent));
    ui->label_cost->setText(QString::number(ga->currentCost));
    gMap->showPath(ga->m_pop.first());
    qInfo()<<"cost:"<<ga->m_pop.first().cost<<" "<<ga->m_pop.first();
    if(!ga->full) ui->progressBar->setValue(float(ga->gCurrent)/ga->gMax*100);
}

void MainWindow::on_checkBox_full_stateChanged(int arg1)
{
    if(arg1==Qt::Checked) ui->spin_genNum->setEnabled(false);
    else ui->spin_genNum->setEnabled(true);
}

